/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyyoona7.picker;

import com.zyyoona7.picker.listener.DialogPickerValueChange;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * 描述
 *
 * @author name
 * @since 2021-04-09
 */
public class CityPickerDialogFragment {
    private static final int THREE = 3;
    private Context context;
    private CommonDialog mCommonDialog = null;
    private DialogPickerValueChange dialogPickerValueChange;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public CityPickerDialogFragment(Context context) {
        this.context = context;
        create();
    }

    private void create() {
        if (mCommonDialog == null) {
            Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
            int width = display.getAttributes().width;
            int height = display.getAttributes().height;
            mCommonDialog = new CommonDialog(context);
            Component component1 = LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_ability_picture, null, true);
            initComponent(component1, mCommonDialog);
            mCommonDialog.setSize(width, height / THREE);
            mCommonDialog.setContentCustomComponent(component1);
            mCommonDialog.setAutoClosable(true);
            mCommonDialog.setAlignment(LayoutAlignment.BOTTOM);
        }
    }

    private void initComponent(Component component1, final CommonDialog commonDialog) {
        final Button cancelBtn = (Button) component1.findComponentById(ResourceTable.Id_btn_cancel);
        final Button okBtn = (Button) component1.findComponentById(ResourceTable.Id_btn_ok);
        final CityPicker cityPicker = (CityPicker) component1.findComponentById(ResourceTable.Id_opv_city);
        cancelBtn.setClickedListener(component -> {
            if (mCommonDialog != null) {
                mCommonDialog.remove();
            }
        });
        okBtn.setClickedListener(component -> {
            if (dialogPickerValueChange != null) {
                dialogPickerValueChange.valueChange(cityPicker.getProvinceValue()
                        + ","
                        + cityPicker.getCityValue()
                        + ","
                        + cityPicker.getCountyValue());
            }
            if (mCommonDialog != null) {
                mCommonDialog.remove();
            }
        });
    }

    /**
     * 显示show
     */
    public void show() {
        if (mCommonDialog != null) {
            mCommonDialog.show();
        }
    }

    /**
     * setDialogPickerValueChange
     *
     * @param dialogPickerValueChange
     */
    public void setDialogPickerValueChange(DialogPickerValueChange dialogPickerValueChange) {
        this.dialogPickerValueChange = dialogPickerValueChange;
    }
}
